﻿/******************************************************************************
 * Copyright (C) 671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef REALMD_AUTH_AUTHSESSION_H_
#define REALMD_AUTH_AUTHSESSION_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <ctime>
#include <NodeCpp/Macros.h>
#include <NodeCpp/ByteBuffer.h>
#include <NodeCpp/LockedQueue.h>
#include <NodeCpp/Crypto/BigNumber.h>
#include <NodeCpp/Crypto/Sha1.h>
#include <NodeCpp/Crypto/SRP6.h>
#include "Shared/Typedef.h"
#include "Shared/Auth/AuthPacket.h"
#include "Shared/Common.h"
using NodeCpp::LockedQueue;
using NodeCpp::ByteBuffer;
using NodeCpp::Crypto::BigNumber;
using NodeCpp::Crypto::Sha1Hash;
using NodeCpp::Crypto::SRP6;

class AuthSocketDelegate;

class AuthSession
{
public:
    AuthSession(AuthSocketDelegate* _Sock);
    ~AuthSession(void);

    void QueuePacket(AuthPacket* _Packet);
    bool Update(time_t _Diff);

    bool HandlePacket(AuthPacket*);
    bool HandleLogonChallenge(AuthPacket*);
    void HandleLogonChallenge(SqlQueryHolder* _Holder);
    bool HandleLogonProof(AuthPacket*);
    bool HandleReconnectChallenge(AuthPacket*);
    void HandleReconnectChallenge(QueryResult* _Result);
    bool HandleReconnectProof(AuthPacket*);
    bool HandleRealmList(AuthPacket*);
    void HandleRealmList(QueryResult* _Result);

    int GetID(void) const { return PeerID_; }

private:
    void _HandleBanned(void);
    void _SetVSFields(const std::string& _RI);
    void _SendProof(Sha1Hash& _Sha);
    void _LoadRealmlist(QueryResult* _Result, ByteBuffer& _Pkt);
    void _Uninitialize(void);
    typedef LockedQueue<AuthPacket*> PacketQueue;

    AuthSocketDelegate* Sock_;
    PacketQueue Packets_;
    bool Running_;

    int PeerID_;
    std::string PeerHost_;
    int PeerPort_;

    time_t UnauthLifeTime_;
    bool Authed_;

    std::uint32_t AccountId_;
    std::string LoginName_;
    std::string SafeLoginName_;
    std::uint16_t Build_;
    std::string LocalizationName_;
    AccountTypes AccountsecLv_;

    BigNumber N_, s_, g_, v_;
    BigNumber b_, B_;
    BigNumber K_;
    BigNumber ReconnectProof_;

    std::uint32_t FailedLogins_;
    DISALLOW_COPY_AND_ASSIGN(AuthSession);
};

#endif // REALMD_AUTH_AUTHSESSION_H_
